Shader "Level4/Rim/SimpleRimAlpha" {
	Properties {
		_MainTex("MainTexture",2d)=""{}
		_RimColor("Rim Color",color) = (1,1,1,1)
		_RimPower("Rim Power",float) = 2
		_Atten("Atten",float) = 2
		
		_LambertPower("Lambert Power",float) = 2
		_SelfPower("Self Power",float) = 1
		_CustomLight0("CustomLight",color) = (.3,.3,.3,1)
		_Alpha("Alpha",float) = 1
		
		_BRDFPower("BrdfPower",float) = 0
	}
	
	SubShader {
		Tags { "RenderType"="Transparent" }
		LOD 200
		Blend one one //SrcAlpha OneMinusSrcAlpha
		
		CGPROGRAM
		#pragma surface surf HalfLambertRim alpha

		sampler2D _MainTex;

		struct Input {
			float2 uv_MainTex;
		};
		half4 _RimColor;
		half _RimPower;
		half _Atten;
		half _LambertPower;
		half _SelfPower;
		half4 _CustomLight0;
		half _Alpha;
		half _BRDFPower;
		
		inline fixed4 LightingHalfLambertRim(SurfaceOutput s, half3 lightDir,half3 viewDir,fixed atten){
			//计算 半角向量
			half3 halfVector = normalize(lightDir+viewDir);
			
			fixed NdotL = max(0,dot(s.Normal,lightDir));
			
			// fixed EdotH = max(0,dot(viewDir,halfVector));
			fixed NdotH = max(0,dot(s.Normal,halfVector));
			fixed NdotE = max(0,dot(s.Normal,viewDir));
			
			fixed halfLambert = pow(NdotL * 0.5 + 0.5,_LambertPower);
			fixed rimLight = 1 - NdotE;
			rimLight = pow(rimLight,_RimPower) * NdotH;
			
			//tex brdf
			half4 brdf = tex2D(_MainTex,half2(NdotE,0.5));
			brdf *= _BRDFPower;
			
			fixed4 c;
			c.rgb = (s.Albedo * brdf.rgb * _CustomLight0.rgb * _SelfPower) * (halfLambert * atten * _Atten);
			c.rgb += rimLight * _RimColor;
			c.a = s.Alpha;
			return c;
		}

		void surf (Input IN, inout SurfaceOutput o) {
			half4 c = tex2D (_MainTex, IN.uv_MainTex);
			o.Albedo = c.rgb;
			o.Alpha = c.a * _Alpha;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}
